package com.example.app2024.sqlite;

import android.content.ContentValues;
import android.content.DialogInterface;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.example.app2024.R;

public class SqliteActivity extends AppCompatActivity implements View.OnClickListener
{
      private EditText etname, etphone;
      private Button btnadd, btndel, btnquery, btnupdate;
      private SqliteHelper myHelper;
      private TextView textView;

      @Override
      protected void onCreate(Bundle savedInstanceState)
      {
            super.onCreate(savedInstanceState);
            EdgeToEdge.enable(this);
            setContentView(R.layout.activity_sqlite);
            ViewCompat.setOnApplyWindowInsetsListener(
                    findViewById(R.id.main), (v, insets) -> {
                          Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
                          v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
                          return insets;
                    });

            myHelper = new SqliteHelper(this);
            textView = findViewById(R.id.textView);

            etname = findViewById(R.id.editTextText);
            etphone = findViewById(R.id.editphone);
            btnadd = findViewById(R.id.btn_add);
            btndel = findViewById(R.id.btn_del);
            btnquery = findViewById(R.id.btn_query);
            btnupdate = findViewById(R.id.btn_update);

            btnupdate.setOnClickListener(this);
            btnquery.setOnClickListener(this);
            btndel.setOnClickListener(this);
            btnadd.setOnClickListener(this);
      }

      @Override
      public void onClick(View v)
      {
            if (v.getId() == R.id.btn_add)
            {

                  SQLiteDatabase db = myHelper.getWritableDatabase();
                  ContentValues values = new ContentValues();
                  values.put("name", etname.getText().toString().trim());
                  values.put("phone", etphone.getText().toString().trim());

                  db.insert("info", null, values);
                  db.close();
                  AlertDialog.Builder builder = new AlertDialog.Builder(this);
                  builder.setTitle("添加成功");
                  builder.setMessage("成功添加一条用户信息");
                  builder.setPositiveButton(
                          "确定", new DialogInterface.OnClickListener()
                          {
                                @Override
                                public void onClick(DialogInterface dialog, int which)
                                {
                                      dialog.dismiss();
                                }
                          });

                  builder.create().show();
            }

            if (v.getId() == R.id.btn_query)
            {
                  SQLiteDatabase database = myHelper.getReadableDatabase();
                  String name = etname.getText().toString().trim();
                  Cursor  cursor = null;
                  if (name.length() > 0)
                  {
                        //比较麻烦,可以直接使用execsql 命令
                        cursor = database.query("info" , null , "name=?" ,new String[]{name} , null , null, null);
                  }else
                  {
                        cursor = database.query("info", null, null, null, null, null, null);
                  }

                  if (cursor.getCount() == 0)
                  {
                        textView.setText("没有记录");
                  } else
                  {
                        cursor.moveToFirst();
                        textView.setText("姓名:" + cursor.getString(1)+ "   "  + "电话:" + cursor.getString(2) + "\n");
                        while (cursor.moveToNext())
                        {
                              textView.append(  "姓名:" + cursor.getString(1)+ "   "  + "电话:" + cursor.getString(2) + "\n");
                        }
                        cursor.close();
                        database.close();
                  }

            }
      }
}